import pymysql

# 1. 拨号连接
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                             database='hqyj',
                             port=3306,
                             charset='utf8')

# 2. 获取游标
cursor = connection.cursor()

# 3. 发送指令
sql = "select * from users;"
cursor.execute(sql)

# sql = "select * from users where user_name=%s;"
# cursor.execute(sql, ['张三'])
#
# sql = "select * from users where user_name=%(name)s;"
# cursor.execute(sql, {"name": "张三"})

# 4. 听回复
result = cursor.fetchall()   # 获取所有（行）结果
print(result)

# 5. 挂电话
cursor.close()
connection.close()
